$(document).ready(function () {
    initArticle();
})

function deleteArticle(id) {
    if(window.confirm('你确定删除吗？')){
        $.ajax({
            url:"/api/deletearticle/"+id,
            type:"Delete",
            dataType:"json",
            success:function(data){
                initArticle();
                layer.msg(data.message,{icon: 5});
            },
            error:function () {
                layer.msg("系统错误！",{icon: 5});
            }
        });

    }
}

function initArticle() {
    $.ajax({
        type: 'get',
        url: '/api/article',
        dataType:"json",
        data:{
          "pageSize":20,
          "pageNumber":0
        },
        success: function (result) {
            var data = result.data;
            $("#article_list").html("")
            $("#article_account").html(result.totalCount)
            var val = "";
            data.forEach(function(value,index){
                var type =""
                if (value.articleType=="1"){
                    type="技术分享"
                } else if (value.articleType=="2") {
                    type="生活点滴"
                }else {
                    type="更新公告"
                }
                val = val+ "<tr><td><a href=\"/context/"+value.id+"\" class=\"btn_a\">"+value.title+"</a>" +
                    "</td><td>"+type+"</td><td>"+value.createTime+"</td><td>"+value.clickRate+"</td><td>" +
                    "<a role=\"button\" href=\"/admin/modify/"+value.id+"\" class=\"btn_edit\"  style=\"border: 1px solid #F37B1D;padding: 2px 4px;margin: 0px 4px;\">" +
                    "<span class='glyphicon glyphicon-file'></span>编辑</a>" +
                    "<a role=\"button\" class=\"btn_delete\" onclick='deleteArticle("+value.id+")' style=\"border: 1px solid #e7505a;padding: 2px 4px;margin: 0px 4px;\">" +
                    "<span class='glyphicon glyphicon-trash'></span>删除</a></td></tr>";
            });
            $("#article_list").append(val)
        },
        error:function () {
            layer.msg("系统错误！",{icon: 5});
        }
    });
}

